export default {
	namespaced:true,
	state:{
    // 从本地读取token
        token: uni.getStorageSync('token'),
        userInfo: uni.getStorageSync('userInfo')
  },
	mutations:{
    // 设置token
        setToken(state,data){
            // 设置给state
            state.token = data
            // 设置到本地存储
            uni.setStorageSync('token',data)
        },
        // 设置用户资料
        setUserInfo(state,data){
            state.userInfo = data
            uni.setStorageSync('userInfo',data)
        }
  },
	actions:{
    async wxLoginAction(context) {
      uni.login({
        success: async (res) => {
          //发起请求
          const result = await uni.$http.post('/auth/loginByWeixin',{
						code:res.code
					})
          //存储token
          context.commit('setToken',result.token)
          // 发起获取用户资料请求
          let result2 = await uni.$http.get('/user/profile')
          context.commit('setUserInfo',result2)
        },
        fail: (err) => {
          return console.log(err);
        }
      })
  }
  }	
}